<?php
/**
 * Home_Controller
 * - Controller for module page Home
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller

class Home_Controller extends Controller {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->errorArr = array();
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	
    	// Process init
        $objView = new View ();
        $this->tpl_title = '.::MUA THẺ NHANH::.';
        $this->home = true;
        $this->SanPhamA = array();
        Session::Set('MUA', '1');
        $ojbDanhMuc = new DanhMuc();
        $ojbSpCate = new SanphamCategory();
        $ojbSSpCate = new SanphamSCategory();
        $ojbSanPham = new MSanpham();
        $ojbKhoHang = new KhoHang();
        
        $fieldDis = "c.name as nameC, s.id, s.danhmuc_id, s.iddm, s.iddmc, s.name, s.code, s.gia1, s.gia2, s.check_km, s.soluong, s.txtKM";
        $this->spDisplay = $ojbSanPham->sanPhamHome(array('s.check_dis'=>array('value'=>'1', 'math'=>'=')), $fieldDis, "s.seq");

    	$danhMucA = $ojbDanhMuc->getAllByAttribute(array());
    	$countDM = count ($danhMucA);
		if ($countDM > 0) {
			$i=0;
			foreach ($danhMucA as $danhmuc) {
		    	$dataCateA = $ojbSpCate->getListAll($danhmuc['id']);
		    	if (count($dataCateA) > 0) {
		    		foreach ($dataCateA as $dataC) {
		    			$dataC['danhmuc_id'] = $danhmuc['id'];
		    			$dataC['value'] = '';
		    			$this->SanPhamA[$i] = $dataC;
		    			$dataSCate = $ojbSSpCate->getListAll($danhmuc['id'], $dataC['id'], true);
		    			if (count ($dataSCate) > 0) {
		    				$j=0;
		    				foreach ($dataSCate as $dataSub) {
		    					$dataSub['value'] = '';
		    					$this->SanPhamA[$i]['value'][$j] = $dataSub;
		    					$dataSP = $ojbSanPham->getListAll($danhmuc['id'], $dataC['id'], $dataSub['id']);
		    					if (count ($dataSP) > 0) {
		    						foreach ($dataSP as $data) {
		    							if ($danhmuc['id'] == '1') {
		    								$data['soluong'] = $ojbKhoHang->totalSanPham($data['id']);
		    							}
		    							$this->SanPhamA[$i]['value'][$j]['value'][] = $data;
		    						}
		    					} else {
		    						$this->SanPhamA[$i]['value'][$j]['value'] = array();
		    					}
		    					$j++;
		    				}
		    			} else {
		    				$dataSP = $ojbSanPham->getListAll($danhmuc['id'], $dataC['id'], $ojbSSpCate->getIDDMC($danhmuc['id'], $dataC['id']));
		    				if (count ($dataSP) > 0) {
		    					foreach ($dataSP as $data) {
		    						$this->SanPhamA[$i]['value'][] = $data;
		    					}
		    				} else {
		    					$this->SanPhamA[$i]['value'] = array();
		    				}
		    			}
		    			$i++;
		    		}
		    	}
			}
		}   
        //echo"<pre>";print_r($this->SanPhamA);exit;
        
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionService
     */
    public function actionService() {
    	
    	// Process init
        $objView = new View ();
        $this->dichVu = true;
        $this->tpl_title = '.::MUA THẺ NHANH &rarr; DỊCH VỤ::.';
        $this->Service = array();
        $this->nameDV = '';
        
        $ojbSer = new Service();
        $this->id = isset($_GET['id']) ? $_GET['id'] : '';
        if ($this->id != '') {
        	$this->Service = $ojbSer->getByPrimaryKey($this->id);
        	$this->nameDV = !empty($this->Service) ? $this->Service['name'] : '';
        }
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionContact
     */
    public function actionContact() {
    	
    	// Process init
        $objView = new View ();
        $this->lienHe = true;
        $this->tpl_title = '.::MUA THẺ NHANH &rarr; LIÊN HỆ::.';
        $this->buttonCot = false;
        
		$this->getPost = FunctionCommon::getPost(array('hoten', 'email', 'noidung', 'captcha'));
		
		if (isset($_POST['butSub'])) {
			if (!Validator::isEmail($this->getPost['email'])) {
				$this->errorArr['email'] = 'Bạn nhập không đúng định dạng mail';
			} 
			if ($this->getPost['captcha'] != $_SESSION['CAPTCHA']) {
				$this->errorArr['captcha'] = 'Bạn nhập mã chưa đúng';
			}
			
			if(count($this->errorArr) == 0) {
				$sendMai = array(array('name'=>$this->getPost['hoten'], 'mail'=>$this->getPost['email']));
        		$subJ = 'Thông tin từ khách hàng ' . $this->getPost['hoten'];
        		$body = $this->getPost['noidung'];
        		$checkSend = FunctionCommon::sendMail($sendMai, $subJ, $body);
        		if ($checkSend) {
        			$this->buttonCot = true;
        		} else {
        			$this->errorArr = array($checkSend);
        		}
			}
		}
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionTutorial
     */
    public function actionTutorial() {
    	
    	// Process init
        $objView = new View ();
        $this->huongDan = true;
        $this->tpl_title = '.::MUA THẺ NHANH &rarr; HƯỚNG DẪN::.';
       
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionPrice
     */
    public function actionPrice() {
    	
    	// Process init
        $objView = new View ();
        $this->giaCuoc = true;
        $this->tpl_title = '.::MUA THẺ NHANH &rarr; GIÁ CƯỚC::.';
       
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
}
?>

